package cn.king.org.service.impl;

import cn.king.basic.service.impl.BaseServiceImpl;
import cn.king.basic.util.Md5Utils;
import cn.king.basic.util.StrUtils;
import cn.king.org.domain.Employee;
import cn.king.org.mapper.EmployeeMapper;
import cn.king.org.service.EmployeeServiceI;
import cn.king.user.domain.Logininfo;
import cn.king.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;

/**
 * EmployeeServiceImpl实现类
 */
@Service /** 1.将当前类交给Spring管理 2.实例化对象注解 3.创建对象的疏解；前提：扫描包路径 */
/** 支持当前事务，如果没有当前事务，就以非事务方法执行。 */
//@Transactional(readOnly = true,propagation = Propagation.SUPPORTS) 事务可以继承，直接抽取到公共类
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements EmployeeServiceI {
    @Autowired
    private LogininfoMapper logininfoMapper;
    @Autowired
    private EmployeeMapper employeeMapper;

    @Transactional
    @Override
    public void add(Employee employee) {
        //处理盐值和密码
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = Md5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setSalt(salt);
        employee.setPassword(md5Pwd);

        //先添加logininfo
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee,logininfo);
        logininfo.setType(0);//管理员
        logininfo.setDisable(true);
        logininfoMapper.add(logininfo);//获取到自增ID

        //添加user的数据
        employee.setLogininfo_id(logininfo.getId());
        super.add(employee);

        //为当前员工添加角色信息
        if (employee.getRole_id() != null){
            employeeMapper.addRole(employee);
        }
    }

    @Transactional
    @Override
    public void delete(Long id) {
        //先查询登陆信息id
        Employee employee = employeeMapper.getOne(id);
        //删除logininfo
        logininfoMapper.delete(employee.getLogininfo_id());

        //删除当前员工的角色信息
        employeeMapper.delRoleByEmployeeId(id);

        //删除employee
        super.delete(id);
    }

    @Transactional
    @Override
    public void update(Employee employee) {
        //处理盐值和密码
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = Md5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setSalt(salt);
        employee.setPassword(md5Pwd);

        //查以前的老数据：employee里面大的数据来自于前端  -就不会有logininfo_id,shop_id
        Employee oldEmployee = employeeMapper.getOne(employee.getId());//loginfo_id

        //修改user信息
        employee.setLogininfo_id(oldEmployee.getLogininfo_id());
        employee.setShop_id(oldEmployee.getShop_id());
        super.update(employee);

        //修改登陆信息
        Logininfo oldLogininfo = logininfoMapper.getOne(oldEmployee.getLogininfo_id());
        BeanUtils.copyProperties(employee,oldLogininfo);
        oldLogininfo.setId(oldEmployee.getLogininfo_id());
        logininfoMapper.update(oldLogininfo);

        //修改当前员工的角色
        if (employee.getRole_id() != null){
            //先删除再添加
            employeeMapper.delRoleByEmployeeId(employee.getId());
            employeeMapper.addRole(employee);
        }
    }

    //批量删除
    @Override
    public void patchDelete(List<Long> ids) {
        for (Long id : ids) {
            delete(id);
        }
    }

    @Override
    public List<Employee> queryByShopId(Long shopId) {
        return employeeMapper.queryByShopId(shopId);
    }
}
